package org.ourgrid.broker.commands.executors;

import org.ourgrid.broker.commands.SchedulerData;
import org.ourgrid.broker.dao.WorkerDAO;
import org.ourgrid.common.interfaces.Worker;
import org.ourgrid.worker.controller.actions.GetFileInfoMessageHandle;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;
import br.edu.ufcg.lsd.commune.identification.DeploymentID;

public class GetFileInfoExecutor extends SchedulerCommandExecutor {
	
	private long handleId;
	private String destinationFileName;
	private String workerID;
	private long requestID;

	/**
	 * <GET_FILE_INFO handleId='long' localFileName='String' description='String' 
	 * workerID='String' requestID='long' destinationFileName='String'/> 
	 */
	public void execute(SchedulerData data, ServiceManager manager) {
		
		fillData(data.getXml());
		
		DeploymentID workerDeploymentID = new DeploymentID(workerID);
		Worker worker = manager.getDAO(WorkerDAO.class).getWorker(workerDeploymentID);
		
		if (worker != null) {
			worker.sendMessage(new GetFileInfoMessageHandle(handleId, requestID, destinationFileName));
		}	
	}
	
	private void fillData(String xml) {
		
		Document logDocFile = super.getXMl(xml);
		
		Element element = logDocFile.getDocumentElement();
		
		handleId = Long.valueOf(element.getAttribute("handleId"));
		destinationFileName = element.getAttribute("destinationFileName");
		workerID = element.getAttribute("workerID");
		requestID = Long.valueOf(element.getAttribute("requestID"));
	}
}
